package edu.vista;

import java.io.IOException;

import javax.bluetooth.RemoteDevice;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;

import edu.logica.ManejadorBluetooth;
import edu.logica.util.CentralDatos;
import edu.logica.util.Constantes;

public class FormListRemoto extends Formulario {

    List lista;
    private RemotoRenderer fcr;
    private Command atras, conectar;

    public FormListRemoto() {
        conectar = new Command("Conectar");
        atras = new Command("Atras");
        addCommand(atras);
        fcr = new RemotoRenderer();
        addCommand(conectar);

        setCommandListener(this);
    }

    private List createList(RemoteDevice[] listadoDispositivos) {

        RemoteDevice[] listaD = listadoDispositivos;

        List list = new List(listaD);

        list.setFixedSelection(List.FIXED_LEAD);
        list.getStyle().setBgTransparency(0);
        list.setListCellRenderer(fcr);
        return list;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof Command) {
            if (((Command) e.getSource()).equals(atras)) {
                AdminIntefaz.getAdminIntefaz().adelante = false;
                AdminIntefaz.getAdminIntefaz().setCurrent(Constantes.MENU_PRINCIPAL);

            } else if (((Command) e.getSource()).equals(conectar)) {
                CentralDatos.buscando = true;
                new Loader();
                CentralDatos.dispositivo_seleccionado = lista.getSelectedIndex();

                Exception a = ManejadorBluetooth.getManejadorBluetooth().buscarServicios();
                if (a != null) {
                    Dialogo.getDialogo().mostrarDialogoInformacion("se produjo un error conectando con el servidor bluetooth: " + a);
                }
            }
        }

    }

    class RemotoRenderer extends Container implements ListCellRenderer {

        private Label codCompra = new Label("");
        private Label focus = new Label("");

        public RemotoRenderer() {

            codCompra.setTickerEnabled(true);

            setLayout(new BorderLayout());
            Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
            codCompra.getStyle().setBgTransparency(0);
            cnt.addComponent(codCompra);
            addComponent(BorderLayout.CENTER, cnt);
            focus.getStyle().setBgTransparency(100);
            focus.getStyle().setFgColor(0XFFFFFF);
            focus.getStyle().setBgColor(0X696969);
        }

        public Component getListCellRendererComponent(List list, Object value,
                int index, boolean isSelected) {

            RemoteDevice compra = (RemoteDevice) value;

            try {
                codCompra.setText(compra.getFriendlyName(false));
            } catch (IOException e) {
                e.printStackTrace();
            }


            return this;
        }

        public Component getListFocusComponent(List list) {
            return focus;
        }

        public Label getFocus() {
            return focus;
        }

        public void setFocus(Label focus) {
            this.focus = focus;
        }
    }

    public void destruir() {
        removeAll();
        removeAllCommands();
        lista = null;

    }

    public void precargar() {
        setTitle("Dispositivos BlueTooth");
        setScrollable(false);
        setLayout(new BorderLayout());

        lista = createList(CentralDatos.getDisRemotos());
        addComponent(BorderLayout.CENTER, lista);

    }
}
